package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.greendao.MusicInfoBean;
import com.lanou3g.musicenjoy.entity.greendao.MusicInfoBeanDao;
import com.lanou3g.musicenjoy.greendao.DaoSingleton;
import java.util.ArrayList;
import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by 李宁 on 15/11/8.
 *我的最爱适配器
 */
public class MyFavoriteAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<MusicInfoBean> infos;
    private QueryBuilder<MusicInfoBean> queryBuilder;


    public MyFavoriteAdapter(Context context, ArrayList<MusicInfoBean> infos) {
        this.context = context;
        this.infos = infos;
        queryBuilder = DaoSingleton.getInstance().getMusicInfoBeanDao().queryBuilder();
    }

    public void longClickRefresh(MusicInfoBean musicInfoBean) {
        infos.remove(musicInfoBean);
        notifyDataSetChanged();
    }
    public void remove(MusicInfoBean musicInfoBean) {
        infos.remove(musicInfoBean);
        String songName = musicInfoBean.getSongname();
        Log.i("---", "---" + songName);
        DeleteQuery<MusicInfoBean> deleteQuery = queryBuilder.where(MusicInfoBeanDao.Properties.Songname.eq(songName)).buildDelete();
        deleteQuery.executeDeleteWithoutDetachingEntities();
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public MusicInfoBean getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_myfavorite, null);
            viewHolder = new ViewHolder();
            viewHolder.myfavorite_num = (TextView) convertView.findViewById(R.id.myfavorite_num);
            viewHolder.songName = (TextView) convertView.findViewById(R.id.myfavorite_songName);
            viewHolder.singer = (TextView) convertView.findViewById(R.id.myfavorite_singer);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MusicInfoBean infoBean = getItem(position);
        viewHolder.myfavorite_num.setText(position + 1+ "");
        viewHolder.songName.setText(infoBean.getSongname());
        viewHolder.singer.setText(infoBean.getSinger());
        return convertView;
    }

    public class ViewHolder {
        private TextView myfavorite_num;
        private TextView songName;
        private TextView singer;
    }
}
